package com.wang.AspectLog;

import cn.hutool.core.util.ObjectUtil;
import com.wang.util.DateUtil;
import com.wang.util.IpUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @description:
 * @author：tuimao
 * @date: 2024/11/11
 */
@Aspect
@Component
public class LogAspect {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Pointcut("execution(public * com.wang.controller.*.*.*(..))")
    public void optLogPointcut() {
    }

    @Around("optLogPointcut()")
    public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
        String nowDate = DateUtil.getNowDate();
//        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//        Method method = signature.getMethod();
        Object[] args = joinPoint.getArgs();
        logger.info("web request info ip:{},start time:{}, args:{}", IpUtil.getIpAddr(),nowDate, ObjectUtil.toString(args));
        Object proceed = joinPoint.proceed();
        String responseDate = DateUtil.getNowDate();
        logger.info("web response info ip:{},end time:{}, result:{}",IpUtil.getIpAddr(),responseDate, ObjectUtil.toString(proceed));
        return proceed;
    }
}
